package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    DishService dishService;
    @Autowired
    SetmealDishService setmealDishService;
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }
    @GetMapping("page")
    public R<Page<SetmealDto>> pageWithDish(Integer page,Integer pageSize,String name){
        Page<SetmealDto> setmealDtoPage=setmealService.pageWithDish(page,pageSize,name);
        return R.success(setmealDtoPage);
    }
    @DeleteMapping
    public R<String> delete(Long[] ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }
    @GetMapping("{id}")
    public R<SetmealDto> show(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.show(id);
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    @PostMapping("status/{status}")
    public R<String> changeStatus(@PathVariable Integer status,Long[] ids){
        setmealService.update().set("status",status).in("id",ids).update();
        return R.success("修改状态成功");
    }
    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        List<Setmeal> list = setmealService.query().eq(categoryId!=null,"category_id", categoryId).eq(status!=1,"status", status).list();
        return R.success(list);
    }
    @GetMapping("dish/{setmealId}")
    public R<List<DishDto>> getDishBySetmealId(@PathVariable Long setmealId){
        List<DishDto> list=dishService.getDishBySetmealId(setmealId);
        return R.success(list);
    }

}
